﻿using System;
using System.Runtime.Serialization;


namespace SW2A311_Lib
{
    /// <summary>
    /// User-strukturen, der laver bruger objekter.
    /// </summary>
    [Serializable]
    public class User
    {
        #region Properties
        public string Username
        {
            get
            {
                return _username;
            }
            set
            {
                int UsernameLength = 3;

                if (string.IsNullOrWhiteSpace(value))
                {
                    throw new ArgumentNullException("Username cannot be empty or whitespace");
                }
                else if (value.Length < UsernameLength)
                {
                    throw new ArgumentOutOfRangeException("Username has to be at least " + UsernameLength + " characters long");
                }
                else _username = value;

            }
        }
        private string _username;

        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                int PasswordLength = 6;

                if (string.IsNullOrWhiteSpace(value))
                {
                    throw new ArgumentNullException("Password cannot be empty or whitespace");
                }
                else if (value.Length < PasswordLength)
                {
                    throw new ArgumentOutOfRangeException("Password must be at least " + PasswordLength + "characters long!");
                }
                else _password = value;
            }
        }
        private string _password;

        public bool isValid { get; private set; }
        #endregion
        #region Constructors
        /// <summary>
        /// Standard constructor til User objekter.
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public User(string username, string password)
        {
            Username = username;
            Password = password;
        }
        /// <summary>
        /// Constructor der også erklærer <c>validIn</c>. 
        /// </summary>
        /// <remarks>
        /// <c>validIn</c> giver <c>isValid</c> en værdi. <c>isValid</c> bruges til at fortælle om en user er blevet accepteret af serveren ved login.
        /// </remarks>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="validIn"></param>
        public User(string username, string password, bool validIn)
        {
            Username = username;
            Password = password;
            isValid = validIn;
        }
        /// <summary>
        /// Construktor til når objektet serialiseres. 
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ctxt"></param>
        public User(SerializationInfo info, StreamingContext ctxt)
        {
            this._username = (string)info.GetValue("_username", typeof(string));
            this._password = (string)info.GetValue("TypeCode", typeof(string));
            this.isValid = (bool)info.GetValue("isValid", typeof(bool));
        }
        /// <summary>
        /// Metode til deserializering af objektet.
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ctxt"></param>
        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("_username", this._username);
            info.AddValue("_password", this._password);
            info.AddValue("isValid", this.isValid);
        }
        #endregion
    }
}
